package com.hehe.controller;

import com.hehe.DTO.Tb_sql_test_DTO;
import com.hehe.Dao.Select_Mapper;
import com.hehe.Dao.Update_Mapper;
import com.hehe.POJO.Tb_sql_test;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.standard.processor.StandardUtextTagProcessor;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/update")
public class Update {

    @Value("${file.upload.path}")
    private String path;

    @Autowired(required = false)
    private Update_Mapper update_mapper;

    @Autowired
    private JdbcTemplate jdbcTemplate;

//    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");
    /*
    <form  method="post"   action="/upload2"  enctype="multipart/form-data">
        <input type="file" name="uploadFiles" value="请选择文件" multiple>
        <input type="submit" value="上传">
    </form>

ysql://localhost:3306/myschool?characterEncoding=utf-8&allowMultiQueries=true

*/




    @PostMapping(value = "/ajax")
    @ResponseBody
    public String sql(Model model, @RequestBody String postObj) throws JSONException {
        System.out.println(postObj);
        JSONObject jsonobj = new JSONObject(postObj);

        // id
        Object id =  jsonobj.get("id");
        System.out.println(id.toString());

        // strrr
        Object strrr =  jsonobj.get("strrr");
        System.out.println(strrr.toString());

        // type
        Object type =  jsonobj.get("type");
        System.out.println(type.toString());   // sql _ answer

        Integer ii = 0;
        if (type.toString().equals("sql")){
            ii = update_mapper.update_sql(strrr.toString(), id.toString());
        }
        if (type.toString().equals("answer")){
            ii = update_mapper.update_answer(strrr.toString(), id.toString());
        }
        if (type.toString().equals("describe")){
            ii = update_mapper.update_describe(strrr.toString(), id.toString());
        }

        if( ii == 1){
            return "success";
        }
        return "false";
    }



    @PostMapping(value = "/level")
    @ResponseBody
    public String level__(Model model, @RequestBody String postObj) throws JSONException {
        System.out.println(postObj);
        JSONObject jsonobj = new JSONObject(postObj);

        // id
        Object id =  jsonobj.get("id");
        System.out.println(id.toString());

        // level
        Object level =  jsonobj.get("level");
        System.out.println(level.toString());

        Integer ii = 0;
        ii = update_mapper.update_level((Integer) level, id.toString());
        if( ii == 1){
            return "success";
        }
        return "false";
    }





}

